﻿using System;
using System.Collections.Generic;
using System.Text;
using Com.Toook.ParseHtml.Entity;
using NSoup.Select;
using NSoup.Nodes;
using Com.Toook.Common;
using System.Text.RegularExpressions;
using Com.Toook.ParseHtml.Enum;
using System.Collections.Specialized;
using System.Data;
using Rule = Com.Toook.ParseHtml.Entity.Rule;

namespace Com.Toook.ParseHtml
{
    public class GetContentResultFromHtml
    {
        private NSoup.Nodes.Document doc = null;
        /// <summary>
        /// 详细内容的解析规则
        /// </summary>
        private DetailRoadMap roadMap;
        /// <summary>
        /// 保存从跳转页面获取的字段名、值
        /// </summary>
        private StringDictionary dicResult = new StringDictionary();
        /// <summary>
        /// 保存内容，有分页的拼接分页内容
        /// </summary>
        private StringBuilder sbFinalContent = new StringBuilder(512);
        /// <summary>
        /// 保存页面的初始根元素
        /// </summary>
        private Elements rootElements = new Elements();
        public GetContentResultFromHtml(DetailRoadMap roadMap)
        {
            this.roadMap = roadMap;
        }
        /// <summary>        
        /// 下载并保存新闻内容       
        /// </summary>
        /// <param name="siteId">RssSites表中的SiteId</param>
        public void GetRssNewsContent(int siteId)
        {
            DataTable urlTable = SaveResultToAccess.GetRssNewsUrl(siteId, roadMap.InsertTableName, roadMap.SqlFields);            
            foreach (DataRow row in urlTable.Rows)
            {
                //1、获取跳转页面的url和需要的字段
                string link = row["link"].ToString();
                
                //添加其他字段值到结果字典
                InitDicResult(row);
                
                //2、最后从内容页面获取内容
                InitRootElements(link, roadMap.contentPage.InitStep);
                bool isGetContentSuccess = true;
                if (this.rootElements.Count == 0)//没有获取到初始节点，即此网页不满足配置文件的解析规则
                {
                    isGetContentSuccess = false;
                    LogHelper.Log.Error("GetRssNewsContent(" + siteId + ")中InitRootElements()没有获取到初始节点. url=" + link);
                }
                else
                {
                    //解析出内容
                    ParseStepByStep(this.rootElements, roadMap.contentPage.rule);
                    if (this.dicResult.Count == 0)
                    {
                        isGetContentSuccess = false;
                        LogHelper.Log.Error("ParseStepByStep时没有获取到内容节点. url=" + link);
                    }
                }
                //解析完内容后，保存到数据库                    
                SaveResultToAccess.SaveRssNewsContent(row["id"].ToString(), roadMap.InsertTableName, isGetContentSuccess, dicResult);
                //if (sbFinalContent.Length > 0)
                //{
                //    sbFinalContent.Remove(0, sbFinalContent.Length);
                //}
                //InitDicResult(row);              
            }
        }
        /// <summary>
        /// 从新闻详细页面提取出内容
        /// </summary>
        /// <param name="link">新闻详细页面url</param>
        /// <returns></returns>
        public string GetNewsContent(string link)
        {
            string content = "";
            //添加其他字段值到结果字典
            //InitDicResult(row);
            dicResult.Clear();
            
            //2、最后从内容页面获取内容
            InitRootElements(link, roadMap.contentPage.InitStep);
            //bool isGetContentSuccess = true;
            if (this.rootElements.Count == 0)//没有获取到初始节点，即此网页不满足配置文件的解析规则
            {
                //isGetContentSuccess = false;
                LogHelper.Log.Error("GetNewsContent(" + link + ")中InitRootElements()没有获取到初始节点. ");
            }
            else
            {
                //解析出内容
                ParseStepByStep(this.rootElements, roadMap.contentPage.rule);
                if (this.dicResult.Count == 0)
                {
                    //isGetContentSuccess = false;
                    LogHelper.Log.Error("ParseStepByStep时没有获取到内容节点. url=" + link);
                }
                else
                {
                    foreach (System.Collections.DictionaryEntry field in dicResult)
                    {
                        if (field.Key.ToString() == "content" && field.Value != null && field.Value.ToString().Length > 0)
                        {
                            content = field.Value.ToString();
                            break;
                        }//内容有多个提取规则时，写多个content
                        else if (field.Key.ToString() == "content2" && field.Value != null && field.Value.ToString().Length > 0)
                        {
                            content = field.Value.ToString();
                            break;
                        }
                        else if (field.Key.ToString() == "content3" && field.Value != null && field.Value.ToString().Length > 0)
                        {
                            content = field.Value.ToString();
                            break;
                        }
                    }
                }
            }
            return content;    
        }
        public void GetContent()//string tableName
        {
            //从数据库获取url、标题等内容
            DataTable urlTable = SaveResultToAccess.GetKindUrlTemp(roadMap.SiteKindId, roadMap.InsertTableName, roadMap.SqlFields);
            string startDetailUrl = "";//数据库中的获取内容的开始Url
            foreach (DataRow row in urlTable.Rows)
            {
                //1、获取跳转页面的url和需要的字段
                string currentPageUrl = row["url"].ToString();
                startDetailUrl = currentPageUrl;

                //添加其他字段值到结果字典
                InitDicResult(row);
                if (roadMap.transferPageList.Count == 0)
                {
                    //2、最后从内容页面获取内容
                    InitRootElements(startDetailUrl, roadMap.contentPage.InitStep);
                    //只解析出内容页面的字段值，下一页的地址根据PageRule获取，不从返回值获取
                    ParseStepByStep(this.rootElements, roadMap.contentPage.rule);//contentUrl = 

                    //3、如果内容有分页，获取后面分页的内容
                    if (roadMap.contentPage.PageRuleList != null)
                    {
                        InitRootElements(startDetailUrl, roadMap.contentPage.InitStep);
                        while (true)
                        {
                            string nextPageUrl = GetNextPageUrl();//从当前页面中获取下一页的地址
                            //如果没有下一页，停止内容的抓取
                            if (string.IsNullOrEmpty(nextPageUrl))
                            {
                                //return;
                                break;//如果没有下一页，跳出while循环
                            }
                            InitRootElements(nextPageUrl, roadMap.contentPage.InitStep);//将rootElements设置为下一页的初始元素
                            currentPageUrl = nextPageUrl;
                            ParseStepByStep(this.rootElements, roadMap.contentPage.rule);//继续对下一页进行解析 
                        }
                    }
                    //解析完内容后，保存到数据库                    
                    SaveResultToAccess.SaveFinalContent(roadMap.SiteKindId, roadMap.InsertTableName, sbFinalContent.ToString(), dicResult, startDetailUrl);
                    if (sbFinalContent.Length > 0)
                    {
                        sbFinalContent.Remove(0, sbFinalContent.Length);
                    }
                    InitDicResult(row);
                }
                else
                {
                    List<string> lastTransferUrls = new List<string>();
                    foreach (TransferPage page in roadMap.transferPageList)
                    {
                        InitRootElements(currentPageUrl, page.InitStep);
                        //获取跳转页数据,得到下一个页面Url地址, 最后一个跳转页面可能有多个url(电影有BD/DVD/HD等)
                        lastTransferUrls = ParseStepByStep(this.rootElements, page.Rule);
                    }

                    foreach (string contentUrl in lastTransferUrls)
                    {
                        //2、最后从内容页面获取内容
                        InitRootElements(contentUrl, roadMap.contentPage.InitStep);
                        //只解析出内容页面的字段值，下一页的地址根据PageRule获取，不从返回值获取
                        ParseStepByStep(this.rootElements, roadMap.contentPage.rule);//contentUrl = 

                        //3、如果内容有分页，获取后面分页的内容
                        if (roadMap.contentPage.PageRuleList != null)
                        {
                            InitRootElements(contentUrl, roadMap.contentPage.InitStep);
                            while (true)
                            {
                                string nextPageUrl = GetNextPageUrl();//从当前页面中获取下一页的地址
                                //如果没有下一页，停止内容的抓取
                                if (string.IsNullOrEmpty(nextPageUrl))
                                {
                                    return;
                                }
                                InitRootElements(nextPageUrl, roadMap.contentPage.InitStep);//将rootElements设置为下一页的初始元素
                                currentPageUrl = nextPageUrl;
                                ParseStepByStep(this.rootElements, roadMap.contentPage.rule);//继续对下一页进行解析 
                            }
                        }
                        //解析完内容后，保存到数据库                    
                        SaveResultToAccess.SaveFinalContent(roadMap.SiteKindId, roadMap.InsertTableName, sbFinalContent.ToString(), dicResult, startDetailUrl);
                        if (sbFinalContent.Length > 0)
                        {
                            sbFinalContent.Remove(0, sbFinalContent.Length);
                        }
                        InitDicResult(row);
                    }
                }
            } 
        }
        /// <summary>
        /// 设置dicResult的属性值
        /// </summary>
        /// <param name="row"></param>
        private void InitDicResult(DataRow row)
        {
            dicResult.Clear();

            if (!string.IsNullOrEmpty(roadMap.SqlFields))
            {
                string[] sqlFields = roadMap.SqlFields.Split(',');
                foreach (string field in sqlFields)
                {
                    dicResult.Add(field.Trim(), row[field.Trim()].ToString());//要trim()掉配置文件的空格，否则row[" column "]会找不到
                }
            }
        }
        /// <summary>
        /// 设置页面初始根元素
        /// </summary>
        /// <param name="url"></param>
        /// <param name="step"></param>
        private void InitRootElements(string url, Step step)
        {
            this.rootElements = GetInitRootElements(url, step);
        }
        
        /// <summary>
        /// 根据分类页面URL和初始步骤，获取第一层 initElements 元素
        /// </summary>
        /// <param name="sourceUrl">分类页面URL</param>
        /// <param name="initStep">初始步骤</param>
        /// <returns></returns>
        private Elements GetInitRootElements(string sourceUrl, Step initStep)
        {
            Elements elements = new Elements();
            try
            { //用html字符串解析的doc对象，不能获取 abs:href

                //string html = Utils.DownloadHtml(sourceUrl, null);
                //if (string.IsNullOrEmpty(html))
                //{
                //    return elements;
                //}
                //else
                //{
                //    try
                //    {
                //        doc = NSoup.NSoupClient.Parse(html);
                //    }
                //    catch (Exception pe)
                //    {
                //        LogHelper.Log.Error("解析html错误。NSoup.NSoupClient.Parse(html)。html=" + html, pe);
                //    }
                //}
                doc = NSoup.NSoupClient.Connect(sourceUrl).Get();
            }
            catch (Exception ex)
            {
                LogHelper.Log.Warn("下载网页失败。GetInitRootElements()。sourceUrl=" + sourceUrl, ex);
                return elements;
            }

            Element body = doc.Body;
            elements = ParseElements.GetElements(initStep, body);
            
            return elements;
        }
        /// <summary>
        /// 获取下一页Url。遍历PageRuleList，用每个可能的规则获取下一页url
        /// </summary>
        /// <returns></returns>
        private string GetNextPageUrl()
        {
            string nextPageUrl = null;
            foreach (PageRule pageRule in roadMap.contentPage.PageRuleList)
            {
                nextPageUrl = GetNextPageUrlString(this.rootElements, pageRule.NextPageRule);
                if (!string.IsNullOrEmpty(nextPageUrl))
                {
                    break;//获取到下一页地址后就退出
                }
            }
            return nextPageUrl;
        }
        /// <summary>
        /// 获取下一页Url地址
        /// </summary>
        /// <param name="initElements"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        private string GetNextPageUrlString(Elements initElements, Rule rule)
        {
            string nextPageUrl = null;
            //1、根据rule、step获取包含下一页链接的元素
            initElements = ParseElements.GetElementsByStep(initElements, rule);

            //2、根据Property获取下一页Url
            if (initElements.Count > 0 && rule.NextProperty())
            {
                while (rule.CurrentProperty.NextStep())//属性中还有需要处理的步骤
                {
                    Step step = rule.CurrentProperty.CurrentStep;
                    initElements = ParseElements.GetAndMergeElements(initElements, step);
                    if (initElements.Count == 0)
                    {
                        break;
                    }
                }
                rule.CurrentProperty.ResetNextStep();
                //获取下一页Url
                nextPageUrl = ParseElements.GetResultStringByResult(initElements, rule.CurrentProperty.Result);
            }
            rule.ResetNextProperty();

            return nextPageUrl;
        }
        /// <summary>
        /// 按照Rule中的Step规则，解析出符合条件的元素列表。供Property获取最终结果用
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        //private Elements GetElementsByStep(Elements elements, Rule rule)
        //{
        //    //elements.Count > 0 判断 GetAndMergeElements 返回的initElements新结果集是否有数据，没有就跳出循环
        //    while (rule.NextStep() && elements.Count > 0)
        //    {
        //        //按照step.GetBy规则，遍历elements，合并每个节点的结果。将新结果赋给elements，迭代下一个step操作。
        //        elements = ParseElements.GetAndMergeElements(elements, rule.CurrentStep);
        //    }
        //    rule.ResetNextStep();
        //    return elements;
        //}
        /// <summary>
        /// 根据配置文件transferPage节点解析出：
        /// 1、跳转到下一页的url(跳转页面根据Property中的url属性节点规则获取；最终的内容页面的下一页不从此返回值获取，而是根据PageRule规则获取) 
        /// 2、需要从跳转页获取的字段
        /// </summary>
        /// <param name="initElements"></param>
        /// <param name="rule"></param>
        /// <returns>下一页列表。（一般只有一个，电影有BD、DVD等多个）</returns>
        private List<string> ParseStepByStep(Elements initElements, Rule rule)
        {
            List<string> nextTransferPageUrl = new List<string>();
            StringBuilder sb = new StringBuilder();

            //1、按照Rule中的Step规则，解析出符合条件的元素列表。供Property获取最终结果用
            initElements = ParseElements.GetElementsByStep(initElements, rule);
            if (initElements.Count == 0)
            {
                return nextTransferPageUrl;
            }

            //将rule/step解析到的Elements保存到stepElements
            Elements stepElements;
            //跳转页面initElements.Count 一般只有1个结果
            if (initElements.Count == 1)
            {
                stepElements = new Elements();
                stepElements.Add(initElements[0]);//从列表中取一个元素放到stepElements中，然后解析出此元素中包含的每个结果属性

                while (rule.NextProperty())
                {
                    while (rule.CurrentProperty.NextStep())//属性中还有需要处理的步骤
                    {
                        Step step = rule.CurrentProperty.CurrentStep;
                        stepElements = ParseElements.GetAndMergeElements(stepElements, step);
                        if (stepElements.Count == 0)
                        {
                            break;
                        }
                    }
                    rule.CurrentProperty.ResetNextStep();

                    //获取最终属性结果
                    string result = ParseElements.GetResultStringByResult(stepElements, rule.CurrentProperty.Result);
                    if (rule.CurrentProperty.PropertyName == "url")
                    {
                        nextTransferPageUrl.Add(result);//取得下一页的url
                    }
                    else if (rule.CurrentProperty.PropertyName.ToLower() == "finalcontent")
                    {
                        sbFinalContent.Append(result);//保存最终内容
                    }
                    else//其他结果属性，添加到结果字典
                    {
                        dicResult.Add(rule.CurrentProperty.PropertyName, result);
                    }

                    //获取一个属性后,stepElements已经只包含一个结果属性，要重新给stepElements赋值为整个结果元素集合
                    stepElements = new Elements();
                    stepElements.Add(initElements[0]);
                }
                rule.ResetNextProperty();
            }
            else//最后一个跳转页面，有多个url的情况
            {
                for (int i = 0; i < initElements.Count; i++)
                {
                    stepElements = new Elements();
                    stepElements.Add(initElements[i]);//从列表中取一个元素放到stepElements中，然后解析出此元素中包含的每个结果属性

                    //第一个属性节点是url节点
                    if (i == 0)//不能合并2个判断条件。 i == 0 && rule.NextProperty()。指针会移动
                    {
                        if (rule.NextProperty())
                        {
                            while (rule.CurrentProperty.NextStep())//属性中还有需要处理的步骤
                            {
                                Step step = rule.CurrentProperty.CurrentStep;
                                stepElements = ParseElements.GetAndMergeElements(stepElements, step);
                                if (stepElements.Count == 0)
                                {
                                    return nextTransferPageUrl;//没有详细页面url,直接返回
                                }
                            }
                            rule.CurrentProperty.ResetNextStep();

                            //获取多个详细页面URL列表
                            nextTransferPageUrl = GetDetailUrlList(stepElements, rule.CurrentProperty.Result);
                        }
                    }
                    //再解析其他属性值
                    stepElements = new Elements();
                    stepElements.Add(initElements[i]);
                    while (rule.NextProperty())
                    {
                        while (rule.CurrentProperty.NextStep())//属性中还有需要处理的步骤
                        {
                            Step step = rule.CurrentProperty.CurrentStep;
                            stepElements = ParseElements.GetAndMergeElements(stepElements, step);
                            if (stepElements.Count == 0)
                            {
                                break;
                            }
                        }
                        rule.CurrentProperty.ResetNextStep();

                        //获取最终属性结果
                        string result = ParseElements.GetResultStringByResult(stepElements, rule.CurrentProperty.Result);
                        //跳转页面，没有url/finalcontent，直接添加其他属性
                        dicResult.Add(rule.CurrentProperty.PropertyName, result);                       

                        //获取一个属性后,stepElements已经只包含一个结果属性，要重新给stepElements赋值为整个结果元素集合
                        stepElements = new Elements();
                        stepElements.Add(initElements[i]);
                    }
                    rule.ResetNextProperty();
                }
            }
            return nextTransferPageUrl;
        }
        /// <summary>
        /// 最后一个跳转页面，有多个url详细页面，获取全部页面的Url列表
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<string> GetDetailUrlList(Elements elements, XmlResult result)
        {
            List<string> urlList = new List<string>();
            if (elements == null || elements.Count < 1)
            {
                return urlList;
            }
            string url = "";
            foreach (Element element in elements)
            {
                switch (result.GetBy)
                {
                    case GetBy.ResultInAttr:
                        url = element.Attr(result.Name);
                        break;
                    case GetBy.ResultInText:
                        url = element.Text();
                        break;
                    case GetBy.ResultInHtml:
                        url = element.Html();
                        break;
                    default:
                        break;
                }
                if (result.ResultFilter != null && result.ResultFilter.Count > 0)
                {
                    url = ParseElements.GetResultStringByFilter(url, result.ResultFilter);
                }
                urlList.Add(url);
            }
            return urlList; 
        }
        /// <summary>
        /// 遍历elements节点，获取每个节点中符合step条件的子节点集合；合并所有子节点集合，作为最终的results结果集
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        //private Elements GetAndMergeElements(Elements elements, Step step)
        //{
        //    Elements results = new Elements();

        //    foreach (Element e in elements)
        //    {
        //        Elements eles = ParseElements.GetElements(step, e);
        //        foreach (Element r in eles)
        //        {
        //            results.Add(r);//将满足Step条件的元素添加到结果集                 
        //        }
        //    }
        //    return results;
        //}
       
        /// <summary>
        /// 获取满足step条件的元素集合
        /// </summary>
        /// <param name="step"></param>
        /// <param name="element"></param>        
        //public Elements GetElements(Step step, Element element)
        //{
        //    Elements results = new Elements();
        //    int index;
        //    switch (step.GetBy)
        //    {
        //        case GetBy.Id:
        //            Element ele = element.GetElementById(step.Name);
        //            if (ele != null && ele.ChildNodes.Count > 0)
        //            {
        //                results.Add(ele);
        //            }
        //            break;
        //        case GetBy.Tag:
        //            results = element.GetElementsByTag(step.Name);
        //            break;
        //        case GetBy.Class:
        //            results = element.GetElementsByClass(step.Name);
        //            break;
        //        case GetBy.ContainText:
        //            results = element.GetElementsContainingText(step.Name);
        //            break;
        //        case GetBy.ContainOwnText:
        //            results = element.GetElementsContainingOwnText(step.Name);
        //            break;
        //        case GetBy.MatchText:
        //            results = element.GetElementsMatchingText(step.Name);
        //            break;
        //        case GetBy.MatchOwnText:
        //            results = element.GetElementsMatchingOwnText(step.Name);
        //            break;
        //        case GetBy.Attr:
        //            results = element.GetElementsByAttribute(step.Name);
        //            break;
        //        case GetBy.AttrStart:
        //            results = element.GetElementsByAttributeStarting(step.Name);
        //            break;
        //        case GetBy.IndexEquals:
        //            int.TryParse(step.Name, out index);
        //            results = element.GetElementsByIndexEquals(index);
        //            break;
        //        case GetBy.IndexLessThan:
        //            int.TryParse(step.Name, out index);
        //            results = element.GetElementsByIndexLessThan(index);
        //            break;
        //        case GetBy.IndexGreaterThan:
        //            int.TryParse(step.Name, out index);
        //            results = element.GetElementsByIndexGreaterThan(index);
        //            break;
        //        //属性名，属性值
        //        case GetBy.AttrValue:
        //            results = element.GetElementsByAttributeValue(step.Name, step.Value);
        //            break;
        //        case GetBy.AttrValueStart:
        //            results = element.GetElementsByAttributeValueStarting(step.Name, step.Value);
        //            break;
        //        case GetBy.AttrValueEnd:
        //            results = element.GetElementsByAttributeValueEnding(step.Name, step.Value);
        //            break;
        //        case GetBy.AttrValueContain:
        //            results = element.GetElementsByAttributeValueContaining(step.Name, step.Value);
        //            break;
        //        case GetBy.AttrValueMatch:
        //            results = element.GetElementsByAttributeValueMatching(step.Name, step.Value);
        //            break;
        //        case GetBy.AttrValueContainDateNow:
        //            string date = DateTime.Now.ToString(step.Value);//通过step.Value指定的格式，格式化当前日期
        //            results = element.GetElementsByAttributeValueContaining(step.Name, date);
        //            break;

        //        case GetBy.RemoveTag:
        //            Elements tagElements = element.GetElementsByTag(step.Name);
        //            foreach (Element tag in tagElements)
        //            {
        //                tag.Remove();
        //            }

        //            results.Add(element);
        //            break;
        //        default:
        //            LogHelper.Log.Error("GetElements(Step step, Element element) GetBy=" + step.GetBy + "不合法。");
        //            break;
        //    }
        //    //应用Step的过滤条件
        //    results = ParseElements.GetElementByFilterList(step.FilterList, results);
        //    return results;
        //}

        /// <summary>
        /// 对所有Elements应用List&lt;Filter&gt;过滤条件
        /// </summary>
        /// <param name="filterList"></param>
        /// <param name="elements"></param>
        /// <returns></returns>
        //private Elements FilterStepElements(List<Filter> filterList, Elements elements)
        //{
        //    Elements results = new Elements();
        //    if (filterList != null && filterList.Count > 0)
        //    {
        //        foreach (Filter filter in filterList)//使用每个过滤条件
        //        {
        //            foreach (Element ele in elements)//过滤出符合条件的元素
        //            {
        //                Element e = ParseElements.GetElementByStepFilter(filter, ele);
        //                if (e != null)
        //                {
        //                    results.Add(e);
        //                }
        //            }

        //            if (results.Count == 0)
        //            {
        //                break;
        //            }
        //            else
        //            {
        //                elements = results;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        return elements;
        //    }
        //    return results;
        //}
        /// <summary>
        /// 判断元素是否满足Step的Filter过滤条件。满足原样返回，不满足返回null
        /// </summary>
        /// <param name="Filter"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        //private Element FilterStepElement(Filter filter, Element element)
        //{
        //    Element result = null;
        //    element.HasAttr("");
        //    switch (filter.GetBy)
        //    {
        //        case GetBy.StepFilterHasAttr:
        //            result = element.HasAttr(filter.Name) ? element : null;
        //            break;
        //        case GetBy.StepFilterHasClass:
        //            result = element.HasClass(filter.Name) ? element : null;
        //            break;
        //        case GetBy.StepFilterOwnTextContains:
        //            result = element.OwnText().Contains(filter.Name) ? element : null;
        //            break;
        //        case GetBy.StepFilterTextContains:
        //            result = element.Text().Contains(filter.Name) ? element : null;
        //            break;
        //        case GetBy.StepFilterHtmlContains:
        //            result = element.Html().Contains(filter.Name) ? element : null;
        //            break;
        //        case GetBy.StepFilterValContains:
        //            result = element.Val().Contains(filter.Name) ? element : null;
        //            break;
        //        case GetBy.RemoveTag:
        //            Elements tagElements = element.GetElementsByTag(filter.Name);
        //            foreach (Element tag in tagElements)
        //            {
        //                tag.Remove();
        //            }
        //            result = element;
        //            break;

        //        default:
        //            LogHelper.Log.Error("FilterStepElement() GetBy=" + filter.GetBy + "不合法。");
        //            break;
        //    }
        //    return result;
        //}
        /// <summary>
        /// 获取最终结果字符串
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        //private string GetResultString(Elements elements, XmlResult result)
        //{
        //    string str = null;
        //    if (elements == null || elements.Count < 1)
        //    {
        //        return str;
        //    }

        //    switch (result.GetBy)
        //    {
        //        case GetBy.ResultInAttr:
        //            str = elements[0].Attr(result.Name);
        //            break;
        //        case GetBy.ResultInText:
        //            str = elements[0].Text();
        //            break;
        //        case GetBy.ResultInHtml:
        //            str = elements[0].Html();
        //            break;
        //        default:
        //            break;
        //    }
        //    if (result.ResultFilter != null && result.ResultFilter.Count > 0)
        //    {
        //        str = ParseElements.GetResultStringByFilter(str, result.ResultFilter);
        //    }

        //    return StringHelper.RemoveMoreWhitespace(str);//删除字符串中多余的空格，只留1个         
        //}

        /// <summary>
        /// 从Tag的字符串[ 属性 / Text / Html ]中获取最终的结果字符串
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        //private string FilterResultString(string tagString, List<Filter> filterList)
        //{
        //    if (filterList == null || filterList.Count < 1)
        //    {
        //        return tagString;//直接返回
        //    }

        //    string result = tagString;
        //    foreach (Filter filter in filterList)
        //    {
        //        if (filter.GetBy == GetBy.ResultFilterBetween)//获取指定字符串之间的字符串，不包含字符串本身
        //        {
        //            int start = result.IndexOf(filter.Name);
        //            if (start != -1)
        //            {
        //                int end = result.IndexOf(filter.Name, start);
        //                if (end != -1)
        //                {
        //                    result = result.Substring(start + filter.Name.Length, end - start - filter.Name.Length);
        //                }
        //            }
        //        }
        //        else if (filter.GetBy == GetBy.ResultFilterReplace)//删除/替换
        //        {
        //            result = result.Replace(filter.Name, filter.Value);
        //        }
        //        else if (filter.GetBy == GetBy.ResultFilterMatch)//通过正则表达式获取结果字符串
        //        {
        //            Regex reg = new Regex(filter.Value, RegexOptions.IgnoreCase);
        //            Match match = reg.Match(result);
        //            if (match.Success)
        //            {
        //                result = match.Result("${result}");
        //            }
        //        }
        //    }

        //    return result;
        //}
    }
}
